package com.yc.baseProgram;

import cn.hutool.core.lang.Tuple;
import com.mysql.cj.util.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * author: yc
 * description: 正则表达式
 * date: 2021/8/13 9:30
 */
public class TestRegex {
    private boolean genericamatcher(String expre, String str) {
        Pattern pattern = Pattern.compile(expre);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }
    //字母数字中文
    public void m1() {
        String str = "123你好A12a.";
        System.out.println(genericamatcher("^[a-z0-9A-Z\u4e00-\u9fa5]+$", str));
    }
    //数字逗号
    public void m2() {
        String str = "0,1,2";
        System.out.println(genericamatcher("^\\d+(,\\d+)*$", str));
    }

    /**
     * 把二维码转成ConnectorID
     * @param qrCode
     * @return ConnectorID 扫码类型0（非小桔充电二维码），1， 2，3，4
     */
    public void convertQr2ConnectorID(String qrCode) {
        try {
            String regex1 = "^hlht://\\w+\\.\\w+/$";//屏上码
            String regex2 = "^https://epower\\.xiaojukeji\\.com/epower/static/resources/xcxconf/XIAOJU\\.\\w+\\.\\w+$";//桩上码、小程序二维码1
            String regex4 = "^https://epower\\.xiaojukeji\\.com/epower/static/resources/xcxconf/DC\\w+$";//DC码
            String regex3 = "^https://epower\\.xiaojukeji\\.com/epower/static/resources/xcxconf/\\w+$";//桩上码、小程序二维码 2

            if (genericamatcher(regex1, qrCode)) {//屏上码
                String connectorID = qrCode.substring(7, qrCode.length() - 1).split("[.]")[0];
                System.out.println(connectorID + " ：" + 1);
            } else if (genericamatcher(regex2, qrCode)) {//桩上码、小程序二维码
                String connectorID = qrCode.substring(qrCode.lastIndexOf(".")+1);
                System.out.println(connectorID + " ：" + 2);
            } else if (genericamatcher(regex4, qrCode)) {//老的DC码
                System.out.println(qrCode + " ：" + 4);
            } else if (genericamatcher(regex3, qrCode)) {//桩上码、小程序二维码
                String connectorID = qrCode.substring(62);
                System.out.println(connectorID + " ：" + 3);
            }

        } catch (Exception e) {
            System.out.println("小桔充电解析二维码异常。qrCode:" + qrCode);
        }

    }

    public static void main(String[] args) {
        TestRegex tr = new TestRegex();
//        tr.m1();
//        tr.m2();

        tr.convertQr2ConnectorID("hlht://TEST0010501.101437000/");
        tr.convertQr2ConnectorID("https://epower.xiaojukeji.com/epower/static/resources/xcxconf/XIAOJU.101437000.04221909157601");
        tr.convertQr2ConnectorID("https://epower.xiaojukeji.com/epower/static/resources/xcxconf/04221909157601");
        tr.convertQr2ConnectorID("https://epower.xiaojukeji.com/epower/static/resources/xcxconf/DC703563");

    }
}
